package com.itheima.day01_text1.controller;

import com.itheima.day01_text1.service.UserService;
import com.itheima.day01_text1.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author: 祺诺
 * @Name; xqf
 * @Date: 2021/3/3 19:48
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("toLogin")
    public ModelAndView toLogin(ModelAndView md) {
        md.setViewName("redirect:/login.jsp");
        return md;
    }

    @RequestMapping("toSave")
    public ModelAndView toSave(ModelAndView md) {
        md.setViewName("redirect:/save.html");
        return md;
    }

    @RequestMapping("save")
    public ModelAndView save(ModelAndView md, String username, String password) {
        User user = new User(username, password);
        boolean result = userService.save(user);
        if (result == false) {
            md.setViewName("redirect:/save.html");
        } else {
            md.setViewName("redirect:/login.html");
        }
        return md;
    }

    @RequestMapping(value = "Login", method = RequestMethod.POST)
    public ModelAndView login(ModelAndView md, String username, String password) {
        User user = new User(username, password);
        boolean result = userService.login(user);
        if (result == false) {
            md.setViewName("redirect:/save.html");
        } else {
            md.setViewName("redirect:/success.html");
        }
        return md;
    }
}
